﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Controls.Containers;
using UIT.Controls;
using System.Web;

namespace UIT.Web.Renderers
{
    class StackPanelRenderer : WebControlRenderer<StackPanel>, IWebRenderer
    {
        public override void Render(StackPanel control)
        {
            WebMetadata metadata = null;
            if (control.Metadata != null)
            {
                metadata = (WebMetadata)control.Metadata;
                if (!string.IsNullOrEmpty(metadata.FormId))
                {
                    writer.WriteStartElement("form");
                    writer.WriteAttributeString("id", metadata.FormId);
                    writer.WriteAttributeString("action", metadata.FormAction ?? HttpContext.Current.Request.RawUrl);
                    writer.WriteAttributeString("method", metadata.FormMethod ?? "POST");
                    writer.WriteStartElement("input");
                    writer.WriteAttributeString("name", "__EVENTTARGET");
                    writer.WriteAttributeString("id", "__EVENTTARGET");
                    writer.WriteAttributeString("type", "hidden");
                    writer.WriteStartElement("input");
                    writer.WriteAttributeString("name", "__EVENTARGUMENT");
                    writer.WriteAttributeString("id", "__EVENTARGUMENT");
                    writer.WriteAttributeString("type", "hidden");
                    writer.WriteEndElement();

                }
            }
            writer.WriteStartElement("table");
            control.RenderFullStyle(this.writer);
            switch (control.Orientation)
            {
                case StackPanelOrientation.Horizontal:
                    writer.WriteStartElement("tr");
                    foreach (var item in control.Children)
                    {
                        writer.WriteStartElement("td");
                        ControlExtensions.RenderControl(this, item);
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    break;
                case StackPanelOrientation.Vertical:
                    foreach (var item in control.Children)
                    {
                        writer.WriteStartElement("tr");
                        writer.WriteStartElement("td");
                        ControlExtensions.RenderControl(this, item);
                        writer.WriteEndElement();
                        writer.WriteEndElement();
                    }
                    break;
            }
            writer.WriteEndElement();
            if (metadata != null)
            {
                if (!string.IsNullOrEmpty(metadata.FormId))
                    writer.WriteEndElement();
            }
        }
    }
}
